package com.hugo.maomi_first.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.List;

/**
 * ������ �ײ��л�tab������
 *
 * @Package com.qianfeng.mgp.utils
 * @�� ��:
 * @�� �� ��: zhangwei
 * @�� ��: 15/1/12
 * @�� �� ��:
 * @�� ��:
 */
public class FragmentTabUtils implements RadioGroup.OnCheckedChangeListener {
    //    public static final int DEFAULT_POSTION = 0;
    private List<Fragment> fragments; // һ��tabҳ���Ӧһ��Fragment
    private RadioGroup rgs; // �����л�tab
    private FragmentManager fragmentManager; // Fragment������Activity
    private int fragmentContentId; // Activity����Ҫ���滻�������id
    private int currentTab; // ��ǰTabҳ������
    private OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener; // �����õ��������л�tabʱ�������µĹ���

    /**
     * @param fragmentManager
     * @param fragments
     * @param fragmentContentId
     * @param rgs
     */
    public FragmentTabUtils(FragmentManager fragmentManager, List<Fragment> fragments, int fragmentContentId, RadioGroup rgs) {
        this.fragments = fragments;
        this.rgs = rgs;
        this.fragmentManager = fragmentManager;
        this.fragmentContentId = fragmentContentId;
        rgs.setOnCheckedChangeListener(this);
    }


    public FragmentTabUtils setDefaultPostion(int postion) {
        if (postion < rgs.getChildCount()) {
            ((RadioButton) rgs.getChildAt(postion)).setChecked(true);
            rgs.setOnCheckedChangeListener(this);
        }
        return this;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        for (int i = 0; i < rgs.getChildCount(); i++) {
            if (rgs.getChildAt(i).getId() == checkedId) {
                Fragment fragment = fragments.get(i);
                FragmentTransaction ft = obtainFragmentTransaction(i);
//                getCurrentFragment().onPause(); // ��ͣ��ǰtab
                getCurrentFragment().onStop(); // ��ͣ��ǰtab
                if (fragment.isAdded()) {
                    fragment.onStart(); // ����Ŀ��tab��fragment onStart()
//                    fragment.onResume(); // ����Ŀ��tab��onResume()
                } else {
                    ft.add(fragmentContentId, fragment);
                    ft.commit();
                }
                showTab(i); // ��ʾĿ��tab
                // ����������л�tab���⹦�ܹ��ܽӿ�
                if (null != onRgsExtraCheckedChangedListener) {
                    onRgsExtraCheckedChangedListener.OnRgsExtraCheckedChanged(radioGroup, checkedId, i);
                }
            }
        }
    }

    /**
     * �л�tab
     *
     * @param idx
     */
    private void showTab(int idx) {
        for (int i = 0; i < fragments.size(); i++) {
            Fragment fragment = fragments.get(i);
            FragmentTransaction ft = obtainFragmentTransaction(idx);
            if (idx == i) {
                ft.show(fragment);
            } else {
                ft.hide(fragment);
            }
            ft.commit();
        }
        currentTab = idx; // ����Ŀ��tabΪ��ǰtab
    }

    /**
     * ��ȡһ����������FragmentTransaction
     *
     * @param index
     * @return
     */
    private FragmentTransaction obtainFragmentTransaction(int index) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        // �����л�����
//        if (index > currentTab) {
//            ft.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_left_out);
//        } else {
//            ft.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_right_out);
//        }
        return ft;
    }

    public int getCurrentTab() {
        return currentTab;
    }

    public Fragment getCurrentFragment() {
        return fragments.get(currentTab);
    }

    public OnRgsExtraCheckedChangedListener getOnRgsExtraCheckedChangedListener() {
        return onRgsExtraCheckedChangedListener;
    }

    public void setOnRgsExtraCheckedChangedListener(OnRgsExtraCheckedChangedListener onRgsExtraCheckedChangedListener) {
        this.onRgsExtraCheckedChangedListener = onRgsExtraCheckedChangedListener;
    }

    /**
     * �л�tab���⹦�ܹ��ܽӿ�
     */
    public static interface OnRgsExtraCheckedChangedListener {
        public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index);
    }
}